package com.landscope.issue.controller.base;

import com.landscope.issue.utils.CacheUtils;
import com.landscope.issue.utils.Const;
import com.landscope.issue.utils.IpKit;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseController {

    public String getIp() {
        return IpKit.getIpAddress(getRequest());
    }

    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return request;
    }

    public HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getResponse();
        return response;
    }

    public String getRequestToken() {
        return getRequest().getHeader(Const.TOKEN_KEY);
    }

    public void clearSysUserCache() {
        CacheUtils.remove(getRequestToken());
    }
}
